﻿//----------------------------------------------------------------
// Desc: 
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2011-12-16
//
// Update Date : 
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once


template < typename _Ty >
class IFTextureObject : public IFTextureFile
{
public:
    IFTextureObject(IFEngine* p, FIDHASH hash)
        :fge(p)
        ,ID(hash)
    {
        nWidth = nHeight = 0;
        fWGene = fHGene = 0;
        srcWidth = srcHeight = 0;
        fge = p;
        nUseTime = FGetTime();
    }
    virtual~IFTextureObject()
    {}

    void FillBySize(FTEXTURE_PTR tex, UINT width, UINT height, UINT src_width, UINT src_height, UINT MipLevels, EF_TEXTURE_TYPE Usage, EF_FORMAT format, EF_POOL Pool);
    void FillByFile(const WCHAR* filename, FTEXTURE_PTR& tex, IFTextureInfo& imginfo);
    void FillByRegister(FTEXTURE_PTR& tex, IFTextureInfo& imginfo);

    virtual bool NeedReset(){ return nPool == ePL_Default; }
    virtual FIDTEXTURE GetTextureID(){ return nTexID; }
    virtual void Release() = 0;
    virtual void Lost() = 0;
    virtual void Reset() = 0;
    virtual FSURFACE_PTR GetSurf() = 0;
    virtual bool SaveFile(const WCHAR* filename, EF_IMAGE_FORMAT format) = 0;
    virtual bool SaveFile(const ACHAR* filename, EF_IMAGE_FORMAT format) = 0;
    virtual void SetDefault() = 0;
    virtual DWORD GetPixel(UINT x, UINT y) = 0;
#if FUEL_OS_WINDOWS
    virtual bool FillByHDC(int dx, int dy, int dw, int dh, HDC hdc, int sx, int sy) = 0;
#endif
    virtual bool DoLock(BYTE mip_level, IFTextureLock& lock, EF_TEXTURE_LOCK flags) = 0;
    virtual void UnLock(BYTE mip_level) = 0;
    virtual FTEXTURE_PTR UseTexture() = 0;
    virtual FTEXTURE_PTR GetData() = 0;
    virtual void SetData(FTEXTURE_PTR& tex) = 0;

    FIDTEXTURE nTexID;          // 贴图ID
    FIDHASH ID;
    _Ty resData;
    IFEngine* fge;
    UINT nMipLevels;            // mipmap 级别参数
    EF_TEXTURE_TYPE nTexType;   // 贴图标记参数
    EF_POOL nPool;              // 贴图内存池参数
    EF_FORMAT nFormat;          // 贴图格式参数
    DWORD nUseTime;             // 上次使用时间
};

typedef IFTextureObject< FTEXTURE_PTR > FTextureFile;

template < typename _Ty >
void IFTextureObject< _Ty >::FillBySize(FTEXTURE_PTR tex, UINT width, UINT height, UINT src_width, UINT src_height, UINT MipLevels, EF_TEXTURE_TYPE tt, EF_FORMAT format, EF_POOL Pool)
{
    SetFilepathW(0);
    SetData(tex);

    nWidth = width;
    nHeight = height;
    fWGene = FGetTextureSizeGene(nWidth);
    fHGene = FGetTextureSizeGene(nHeight);
    srcWidth = src_width;
    srcHeight = src_height;

    nMipLevels = MipLevels;
    nTexType = tt;
    nPool = Pool;
    nFormat = format;
}

template < typename _Ty >
void IFTextureObject< _Ty >::FillByFile(const WCHAR* filename, FTEXTURE_PTR& tex, IFTextureInfo& imginfo)
{
    SetFilepathW(filename);
    SetData(tex);

    nWidth = imginfo.Width;
    nHeight = imginfo.Height;
    fWGene = FGetTextureSizeGene(nWidth);
    fHGene = FGetTextureSizeGene(nHeight);
    srcWidth = imginfo.srcWidth;
    srcHeight = imginfo.srcHeight;

    nMipLevels = imginfo.MipLevels;
    nTexType = eTT_Default;
    nPool = ePL_Managed;
    nFormat = imginfo.Format;
}

template < typename _Ty >
void IFTextureObject< _Ty >::FillByRegister(FTEXTURE_PTR& tex, IFTextureInfo& imginfo)
{
    SetData(tex);

    nWidth = imginfo.Width;
    nHeight = imginfo.Height;
    fWGene = FGetTextureSizeGene(nWidth);
    fHGene = FGetTextureSizeGene(nHeight);
    srcWidth = imginfo.srcWidth;
    srcHeight = imginfo.srcHeight;

    nMipLevels = imginfo.MipLevels;
    nTexType = eTT_Default;
    nPool = ePL_Managed;
    nFormat = imginfo.Format;
}